﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.util;
using Newtonsoft.Json;

namespace HotTopic.service
{
    class DifferTopicNameRedisDbProvider
    {
        private static readonly String SoftwareDifferAcceptStocks = "topics:software:stocks:differname:accept:";
        private static readonly String SoftwareDifferDropStocks = "topics:software:stocks:differname:drop:";
        private static readonly String SoftwareUnProcessStocks = "topics:software:stocks:differname:unprocess:";
        private static readonly String SoftwareProcessStocks = "topics:software:stocks:differname:process:";
        private static readonly String SoftwareDifferNameNonJoin = "topics:software:stocks:differname:nonjoin:";
        private static readonly String SoftwareDifferNameJoin = "topics:software:stocks:differname:join:";
        private static readonly String SoftwareDifferNameJoinName = "topics:software:stocks:differname:joinname:";

        //获取需要添加的股票代码
        //此处需要主题名：主题名生成的key
        public static HashSet<string> ListAcceptStocks(string topicName)
        {
            String key = SoftwareDifferAcceptStocks + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<String>>(result);
        }

        //获取不需要合并的主题列表
        public static HashSet<string> ListNonjoinderTopics()
        {
            String key = SoftwareDifferNameNonJoin;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<string>>(result);
        }
        
        //获取合并后的主题名称表
        public static Dictionary<string,string> ListJoinderTopicsName()
        {
            String key = SoftwareDifferNameJoinName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new Dictionary<string,string>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string,string>>(result);
        }
        
        //获取需要合并的主题列表
        public static HashSet<string> ListJoinderTopics()
        {
            String key = SoftwareDifferNameJoin;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<string>>(result);
        }
        
        //获取需要丢弃的股票代码
        //此处需要主题名：主题名生成的key
        public static HashSet<string> ListDropStocks(string topicName)
        {
            String key = SoftwareDifferDropStocks + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<String>>(result);
        }
        
        //获取未处理的差异股票代码，合并主题时调用
        //此处需要主题名：主题名生成的key
        public static HashSet<string> ListUnProcessStocks(string topicName)
        {
            String key = SoftwareUnProcessStocks + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<String>>(result);
        }
        
        //获取已处理的差异股票代码，合并主题时调用
        //此处需要主题名：主题名生成的key
        public static HashSet<string> ListProcessedStocks(string topicName)
        {
            String key = SoftwareProcessStocks + topicName;
            String result = RedisUtils.HtRedis.GetDatabase().StringGet(key);
            if (String.IsNullOrEmpty(result))
            {
                return new HashSet<string>();
            }
            return JsonConvert.DeserializeObject<HashSet<String>>(result);
        }
        
        //保存需要添加的股票代码，人工处理调用
        //此处需要主题名：主题名生成的key
        public static void CacheAcceptStocks(String topicName, HashSet<string> acceptStocks)
        {
            if (acceptStocks.Count <= 0)
            {
                return;
            }
            String key = SoftwareDifferAcceptStocks + topicName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(acceptStocks));
        }
        
        //保存未处理的差异股票代码，合并主题时调用并更新该记录
        //此处需要主题名：主题名生成的key
        public static void CacheUnProcessStocks(string topicName, HashSet<string> differStocks)
        {
            if (differStocks.Count <= 0)
            {
                return;
            }
            String key = SoftwareUnProcessStocks + topicName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(differStocks));
        }
        
        //保存不需要合并的主题列表，人工处理后存储
        public static void CacheNonjoinderTopics(HashSet<string> topics)
        {
            if (topics.Count <= 0)
            {
                return;
            }
            String key = SoftwareDifferNameNonJoin;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(topics));
        }
        
        //保存需要合并的主题列表，人工处理后存储
        public static void CacheJoinderTopics(HashSet<string> topics)
        {
            if (topics.Count <= 0)
            {
                return;
            }
            String key = SoftwareDifferNameJoin;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(topics));
        }
        
        //保存合并主题后的名字，人工处理后存储
        public static void CacheJoinderName(Dictionary<string,string> dictionary)
        {
            if (dictionary.Count <= 0)
            {
                return;
            }
            String key = SoftwareDifferNameJoinName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(dictionary));
        }
        
        //保存已处理的差异股票代码，人工筛选时调用
        //此处需要主题名：主题名生成的key
        public static void CacheProcessedStocks(string topicName, HashSet<string> differStocks)
        {
            if (differStocks.Count <= 0)
            {
                return;
            }
            String key = SoftwareProcessStocks + topicName;
            RedisUtils.HtRedis.GetDatabase().StringSet(key, JsonConvert.SerializeObject(differStocks));
        }
    }
}